﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;
using System.Configuration;

namespace HaiFang
{
    public partial class Login:Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ltMessage.Text = string.Empty;
            if (!IsPostBack)
            {
                //验证系统合法性
                bool _val = false;
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SN"]))
                {
                    _val = Utility.Verify.CheckValid(ConfigurationManager.AppSettings["SN"], ConfigurationManager.AppSettings["ProductName"]);
                }
                if (!_val)
                {
                    this.ltMessage.Text = "系统未注册或者已过期，请联系管理员！";
                    this.btnLogin.Visible = false;
                    return;
                }
                if (string.Compare(Request.QueryString["type"], "logout", true) == 0)
                {
                    this.txtUserName.Value = string.Empty;
                    this.txtPassword.Value = string.Empty;
                    CustomAuthentication.SignOut();
                    HttpContext.Current.Response.Redirect(CustomAuthentication.LoginUrl, true);
                }
            }
        }

        protected void btnLogin_OnClick(object sender, EventArgs e)
        {
            this.ltMessage.Text = string.Empty;
            string username = this.txtUserName.Value.Trim();
            string password = this.txtPassword.Value;

            bool valid = true;
            if (string.IsNullOrEmpty(username))
            {
                this.txtUserNameRequiredFieldValidator.IsValid = false;
                valid = false;
            }
            if (string.IsNullOrEmpty(password))
            {
                this.txtPasswordRequiredFieldValidator.IsValid = false;
                valid = false;
            }
            if (valid)
            {
                password = Security.Encrypt(password);
                int result=CustomAuthentication.Authenticate(username, password);
                if (result==0)
                {
                    //模拟用户
                    //string windowUser = Security.Decrypt(ConfigurationManager.AppSettings["Usr"].ToString());
                    //string windowDomain = Security.Decrypt(ConfigurationManager.AppSettings["Pwd"].ToString());
                    //string windowPwd = Security.Decrypt(ConfigurationManager.AppSettings["Domain"].ToString());
                    //Impersonate.SetUser(windowUser, windowDomain, windowPwd);		
                    //登录跳转
                    if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                    {
                        Response.Redirect(Request.QueryString["ReturnUrl"]);
                    }
                    else
                    {
                        Response.Redirect(CustomAuthentication.DefaultUrl);
                    }
                }
                else if (result == 100)
                {
                    this.ltMessage.Text = "用户名或密码错误!"; 
                }
                else if (result == 300)
                {

                    this.ltMessage.Text = "该用户尝试登录过多，已被锁定"; 
                }
                else
                {
                    this.ltMessage.Text = "系统错误!";
                }
            }
        }
    }
}
